package main

import (
	"emc_vnx_exporter/exporter"
	"flag"
	"github.com/prometheus/client_golang/prometheus"
	"github.com/prometheus/client_golang/prometheus/promhttp"
	"log"
	"net/http"
)

var (
	Version       = "1.0.0"
	storageMng    = flag.String("storage", "10.10.23.23", "The address of the vnx manager")
	listenAddress = flag.String("web.listen-address", ":9601", "Address to listen on for web interface and telemetry.")
	metricPath    = flag.String("web.telemetry-path", "/metrics", "Path under which to expose exporter.")
	user          = flag.String("user", "admin", "The user used to access the vnx manager")
	password      = flag.String("password", "password", "The password used to access the vnx manager")
	landingPage   = []byte("<html><head><title>EMC VNX Exporter " + Version +
		"</title></head><body><h1>EMC VNX Exporter " + Version + "</h1><p><a href='" + *metricPath + "'>Metrics</a></p></body></html>")
)

func init() {
	prometheus.MustRegister(exporter.HostGauge())
	prometheus.MustRegister(exporter.DiskGauge())
	prometheus.MustRegister(exporter.PortGauge())
	prometheus.MustRegister(exporter.FanGauge())
	prometheus.MustRegister(exporter.SPGauge())
	prometheus.MustRegister(exporter.PowerGauge())
	prometheus.MustRegister(exporter.BBUGauge())
	prometheus.MustRegister(exporter.LCCGauge())
	prometheus.MustRegister(exporter.LunGauge())
	prometheus.MustRegister(exporter.StoragePoolGauge())
	prometheus.MustRegister(exporter.FaultGauge())
}

func main() {
	//解析定义的监听端口等信息
	flag.Parse()
	go exporter.GetAllHostMetrics(*storageMng, *user, *password)
	go exporter.GetDiskMetrics(*storageMng, *user, *password)
	go exporter.GetPortListMetrics(*storageMng, *user, *password)
	go exporter.GetAllFanMetrics(*storageMng, *user, *password)
	go exporter.GetSPListMetrics(*storageMng, *user, *password)
	go exporter.GetAllPowerMetrics(*storageMng, *user, *password)
	go exporter.GetAllBBUMetrics(*storageMng, *user, *password)
	go exporter.GetAllLCCMetrics(*storageMng, *user, *password)
	go exporter.GetAllLunMetrics(*storageMng, *user, *password)
	go exporter.GetStoragePoolMetrics(*storageMng, *user, *password)
	go exporter.GetFaultMetrics(*storageMng, *user, *password)
	http.Handle("/metrics", promhttp.Handler())
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		w.Write(landingPage)
	})

	if err := http.ListenAndServe(*listenAddress, nil); err != nil {
		log.Fatalf("Error starting HTTP server: %s", err)
	}

	log.Fatal(http.ListenAndServe(*listenAddress, nil))
}
